package com.xnew.manager.maintainPlan.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.manager.elevator.entity.ElevatorInfo;
import com.xnew.manager.elevator.mapper.ElevatorMapper;
import com.xnew.manager.maintainPlan.entity.ElMaintainPlan;
import com.xnew.manager.maintainPlan.entity.ElMaintainPlanDetails;
import com.xnew.manager.maintainPlan.mapper.ElMaintainPlanDetailsMapper;
import com.xnew.manager.maintainPlan.mapper.ElMaintainPlanMapper;
import com.xnew.manager.maintainPlan.service.ElMaintainPlanService;
import com.xnew.utils.DataInput;
import com.xnew.utils.DataOutput;
import com.xnew.utils.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @Description: 维保计划
 * @Author: Y70
 * @date 2022/10/24
 */
@Service
public class ElMaintainPlanServiceImpl implements ElMaintainPlanService {

    @Resource
    private ElMaintainPlanMapper elMaintainPlanMapper;
    @Resource
    private ElMaintainPlanDetailsMapper elMaintainPlanDetailsMapper;
    @Resource
    private ElevatorMapper elevatorMapper;

    /**
     * 维保计划列表
     * @param input
     * @return
     */
    @Override
    public List<ElMaintainPlan> queryMaintainPlanList(DataInput input) {
        PageHelper.startPage(input.getPageNum(), input.getPageSize());
        Map param = input.getParam();
        List<ElMaintainPlan> elMaintainPlans = elMaintainPlanMapper.queryMaintainPlanList(param);
        return elMaintainPlans;
    }

    /**
     * 创建电梯时调用的维保计划创建接口
     * @param param
     * @return
     */
    @Override
    public DataOutput createMaintainPlan(Map param, SysUserInfo userInfo) {
        DataOutput out = DataOutput.success();
        String elId = param.get("elId").toString();
//        ElevatorInfo elevatorInfo = elevatorMapper.selectById(elId);
        String contractBeforeSignMaintDate = param.get("contractBeforeSignMaintDate").toString();
        String contractEndDate = param.get("contractEndDate").toString();
        String maintOrgId = param.get("maintOrgId").toString();
        String defaultStartTime = "20:00";
        String defaultEndTime = "24:00";
        // 根据合同签署前最近一次维保的日期集合
        Map<String, Set<String>> maintenanceDateMap = getMaintenanceDate(contractBeforeSignMaintDate,contractEndDate);
        for (String month : maintenanceDateMap.keySet()) {
            Set<String> mateList = maintenanceDateMap.get(month);
            for (String mate : mateList) {
                // 根据维保日期查询是否已有维保计划
                Map<String, Object> mapDetails = new HashMap<>();
                mapDetails.put("maintainDate",mate);
                mapDetails.put("elId",elId);
                List<String> list1 = elMaintainPlanMapper.queryMaintainPlanByMaintainDateAndElId(mapDetails);
                if(list1.isEmpty()){
                    // 根据维保年月+机构，查询机构是否已有当月维保计划
                    Map<String, Object> mapPlan = new HashMap<>();
                    mapPlan.put("yearMonth",month);
                    mapPlan.put("maintOrgId",maintOrgId);
                    List<String> elMaintainPlans = elMaintainPlanMapper.queryMaintainPlanByYearMonthAndOrgId(mapPlan);
                    if(elMaintainPlans.isEmpty()){ // 有当月维保计划
                        //保存维保计划
                        ElMaintainPlan elMaintainPlan = new ElMaintainPlan();
                        String mpId = IdUtils.getIncreaseIdByNanoTime();
                        elMaintainPlan.setMpId(mpId);
                        elMaintainPlan.setYearMonth(month);
                        elMaintainPlan.setStatus("0");
                        elMaintainPlan.setCreatedBy(userInfo.getUserInfoNo());
                        elMaintainPlan.setCreatedTime(DateUtil.now());
                        elMaintainPlan.setUpdatedBy(userInfo.getUserInfoNo());
                        elMaintainPlan.setUpdatedTime(DateUtil.now());
                        elMaintainPlanMapper.insert(elMaintainPlan);
                        //保存维保计划详情
                        saveElMaintainPlanDetails(mpId,elId,mate,defaultStartTime,defaultEndTime,userInfo);
                    }else {
                        String mpId = elMaintainPlans.get(0).toString();
                        saveElMaintainPlanDetails(mpId,elId,mate,defaultStartTime,defaultEndTime,userInfo);

                    }
                }
            }
        }
        return out;
    }

    /**
     * 保存维保计划详情
     * @param mpId 维保计划主键
     * @param elId 电梯表主键
     * @param mate 维保日期
     * @param defaultStartTime 维保开始时间
     * @param defaultEndTime 维保结束时间
     * @param userInfo 登录用户信息
     */
    public void saveElMaintainPlanDetails(String mpId, String elId,String mate, String defaultStartTime, String defaultEndTime, SysUserInfo userInfo){
        ElMaintainPlanDetails elMaintainPlanDetails = new ElMaintainPlanDetails();
        elMaintainPlanDetails.setUid(IdUtils.getIncreaseIdByNanoTime());
        elMaintainPlanDetails.setMpId(mpId);
        elMaintainPlanDetails.setElId(elId);
        elMaintainPlanDetails.setMaintainDate(mate);
        elMaintainPlanDetails.setStartTime(defaultStartTime);
        elMaintainPlanDetails.setEndTime(defaultEndTime);
        elMaintainPlanDetails.setCreatedBy(userInfo.getUserInfoNo());
        elMaintainPlanDetails.setCreatedTime(DateUtil.now());
        elMaintainPlanDetails.setUpdatedBy(userInfo.getUserInfoNo());
        elMaintainPlanDetails.setUpdatedTime(DateUtil.now());
        elMaintainPlanDetailsMapper.insert(elMaintainPlanDetails);
    }




    /**
     * 获取维保日期集合
     * @param lastMaintDate
     * @return
     */
    public Map<String, Set<String>> getMaintenanceDate(String lastMaintDate,String contractEndDate){
        Map<String, Set<String>> map = new HashMap<>();
        LocalDate startDate = LocalDate.parse(lastMaintDate);
        LocalDate endDate = LocalDate.parse(contractEndDate);
        LocalDate day15 = startDate.plus(15, ChronoUnit.DAYS);
        while (day15.isBefore(endDate) || day15.isEqual(endDate)) {
            String month = day15.toString().substring(0,7);
            Set<String> dateSet = map.get(month);
            dateSet.add(day15.toString());
            map.put(month, dateSet);
        }
        return map;
    }
}

